﻿/******************************************************************************************
* 读取MYSQL数据库中的文件,并返回数据流到客户端
* Notices:Copyright (c) 2009 By ntko
******************************************************************************************/
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page language="java" import="java.io.*,java.sql.*,oracle.jdbc.*,java.util.*" %>
<%@ page language="java" import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*"%>
<%@ include file="connectionInfo.jsp"%>

<%
  int id=request.getParameter("id")==null?0:Integer.parseInt(request.getParameter("id").toString().trim());
  byte[] fileData;
  Connection conn=null;
  Statement st=null;
  ResultSet rs=null;
 java.io.InputStream fileDataStream = null;
 javax.servlet.ServletOutputStream myOutputStream = response.getOutputStream();
if(id!=0)
{
	try
	{
		Class.forName(DBDriver);
	}catch(ClassNotFoundException e)
	{out.println("error"+e.getMessage());return;}
	try
	{
		conn = DriverManager.getConnection(ConnStr,userName,userPasswd);  
	} catch(SQLException e)
	{out.println("Error:" + e.getMessage());
	}
	try
	{
		st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
		rs = st.executeQuery("select * from "+officeFileInfoTableName+"  where id="+id);
	} 
	catch(SQLException e)
	{out.println("Error:" + e.getMessage());}
	if(rs.next())
	{  
		response.reset();
		response.setContentType("applcation/octet-stream");
		fileDataStream=rs.getBinaryStream("officedate");
		fileData = new byte[1024];
		int readCount = 0 ;
		out.clear();
		while((readCount=fileDataStream.read(fileData,0,1024))!=-1)
		{
			myOutputStream.write(fileData,0,readCount);
		}
		myOutputStream.flush();
		response.flushBuffer();
	}
	else
	{
		out.println("id不存在");
	}
	if(rs != null)rs.close();
	if(st != null)st.close();
	if(conn != null)conn.close();
}
else{out.println("id错误");}

%>